package com.suliu.practice.io;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

public class RandomAccessFileTest {
	public static void main(String[] args) throws IOException {
		Person p=new Person(1,"HELLO",5.42);
		RandomAccessFile raf=new RandomAccessFile("test.txt","rw");
		p.write(raf);
		Person p2=new Person();
		raf.seek(0);//读的位置重回到文件开头
		p2.read(raf);
		System.out.println(p2);
	}
}
class Person{
	public String toString() {
		return this.id+" "+this.name+" "+this.height;
	}
	int id;
	String name;
	double height;
	public Person(){
		
	}
	
	
	public Person(int id,String name,double height){
		this.id=id;
		this.name=name;
		this.height=height;
	}
	public void write(RandomAccessFile raf) throws IOException{
		raf.writeInt(this.id);
		raf.writeUTF(this.name);
		raf.writeDouble(this.height);
	}
	public void read(RandomAccessFile raf) throws IOException{
		this.id=raf.readInt();
		this.name=raf.readUTF();
		this.height=raf.readDouble();
	}
	
}
